<?php
/**
 * Created by PhpStorm.
 * User: zhangxilu
 * Date: 2017/12/23
 * Time: 下午11:21
 */

namespace backend\controllers;


use common\logic\HouseImageLogic;
use common\models\HouseImageModel;
use Yii;
use yii\helpers\Url;
use yii\web\NotFoundHttpException;

class HouseImageController extends BaseController
{
    public function actionIndex($house_id)
    {


        if (Yii::$app->request->isPost) {

            // $p1 $p2是我们处理完图片之后需要返回的信息，其参数意义可参考上面的讲解
            $p1 = $p2 = [];


            // 如果没有商品图或者商品id非真，返回空
            if (empty($_FILES['HouseImageModel']['name']) || empty($_FILES['HouseImageModel']['name']['img_path'])) {
                return $this->asJson(new \stdClass());
            }

            // 循环多张商品banner图进行上传和上传后的处理
            for ($i = 0; $i < count($_FILES['HouseImageModel']['name']['img_path']); $i++) {
                // 上传之后的商品图是可以进行删除操作的，我们为每一个商品成功的商品图指定删除操作的地址
                $url = '/house-image/delete';

                // 调用图片接口上传后返回的图片地址，注意是可访问到的图片地址哦
                $imageUrl = HouseImageLogic::instance()->saveFile($house_id,$i);

                // 保存商品banner图信息
                $model = new HouseImageModel();
                $model->hid = $house_id;
                $model->img_path = $imageUrl;
                $key = 0;
                if ($model->save(false)) {
                    $key = $model->id;
                }

                // 这是一些额外的其他信息，如果你需要的话
                // $pathinfo = pathinfo($imageUrl);
                // $caption = $pathinfo['basename'];
                // $size = $_FILES['Banner']['size']['banner_url'][$i];


                $p1[$i] = $imageUrl;
                $p2[$i] = ['url' => $url, 'key' => $key];
            }

            return $this->asJson([
                'initialPreview' => $p1,
                'initialPreviewConfig' => $p2,
                'append' => true,
            ]);

        }
        $model = new HouseImageModel();
        $p1 = $p2 = [];

        $images = HouseImageLogic::instance()->getAll($house_id);
        if ($images) {
            foreach ($images as $k => $v) {
                $p1[$k] = $v->img_path;
                $p2[$k] = [
                    // 要删除商品图的地址
                    'url' => Url::toRoute('/house-image/delete'),
                    // 商品图对应的商品图id
                    'key' => $v->id,
                ];
            }
        }
        return $this->render('index', [
            'model' => $model,
            'p1' => $p1,
            'p2' => $p2,
            'house_id' => $house_id
        ]);
    }

    public function actionDelete(){
        // 前面我们已经为成功上传的banner图指定了key,此处的key也即时banner图的id
        if ($id = Yii::$app->request->post('key')) {
            $model = $this->findModel($id);
            $model->delete();
        }
        return $this->asJson(['success' => true]);
    }

    /**
     * Finds the HouseModel model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     *
     * @param integer $id
     *
     * @return HouseImageModel the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = HouseImageModel::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}